package com.infinit.selfhelp.common.widget

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.drake.brv.BindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import com.infinit.selfhelp.common.R
import com.infinit.selfhelp.common.base.BaseDialog
import okhttp3.internal.notifyAll

class CommonListDialog {

    private var adapter: BindingAdapter? = null
    var dialog: Dialog? = null

    /**
     *
     * @param dialogLayoutId dialog view,里面需要有Rv
     * @param rvId
     * @param itemLayoutId itemview，需要是 databindingview
     * @param list 填充的数据
     * */
    inline fun <reified T> buildListDialog(
        context: Context, dialogLayoutId:
        ViewBinding, rvId: Int,
        itemLayoutId: Int,
        list: List<T>,
        crossinline bindAdapter: (bind: BindingAdapter, dialog: CommonListDialog) -> Unit
    ): CommonListDialog {
        val dialog1 = BaseDialog(context).setContent(dialogLayoutId.root).build()
        dialogLayoutId.root.findViewById<RecyclerView>(rvId).linear().setup {
            putBindingAdapter(this)
            addType<T>(itemLayoutId)
            bindAdapter.invoke(this, this@CommonListDialog)
        }.models = list

        dialog = dialog1
        return this
    }

    fun notifyList(position: Int) {
        adapter?.notifyItemRemoved(position)
    }

    fun getBindingAdapter(): BindingAdapter? {
        return adapter
    }

    fun putBindingAdapter(adapter: BindingAdapter) {
        this.adapter = adapter
    }

    fun show() {
        dialog?.show()
    }

    fun dismiss() {
        dialog?.dismiss()
    }
}